package com.whcoding.test.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.whcoding.test.common.BeanUtil;
import com.whcoding.test.common.HutoolPageUtil;
import com.whcoding.test.common.excel.ExcelUtilsTemp;
import com.whcoding.test.dao.OrmCustomerDao;
import com.whcoding.test.dao.mapper.OrmCustomerMapper;
import com.whcoding.test.pojo.OrmCustomerExcelVO;
import com.whcoding.test.pojo.OrmCustomerPO;
import com.whcoding.test.pojo.OrmCustomerVO;
import com.whcoding.test.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-04-18 16:08
 **/
@Service
public class CustomerServiceImpl implements CustomerService {

	private static final String PATH = "D:\\poi_excel\\";

	@Autowired
	OrmCustomerDao ormCustomerDao;

	@Autowired
	OrmCustomerMapper ormCustomerMapper;


	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public Map<String, Object> queryByHutoolPage(Pageable pageable) {
		List<OrmCustomerVO> customerList = getCustomerList();
		return HutoolPageUtil.toPage(
				HutoolPageUtil.toPage(pageable.getPageNumber(), pageable.getPageSize(), customerList),
				customerList.size()
		);
	}

	/**
	 * @param ormCustomerVO
	 * @param response
	 * @throws IOException
	 */
	@Override
	public void exportByHutool(OrmCustomerVO ormCustomerVO, HttpServletResponse response) throws IOException {
		List<OrmCustomerVO> customerList = getCustomerList();
		List<Map<String, Object>> list = new ArrayList<>();
		for (OrmCustomerVO customerVO : customerList) {
			Map<String, Object> map = new LinkedHashMap<>();
			map.put("客户编码", customerVO.getCustomerNo());
			map.put("客户名称", customerVO.getCustomerName());
			map.put("客户地址", customerVO.getCustomerAddress());
			map.put("客户ID", customerVO.getCustomerId());
			map.put("客户类型", customerVO.getCustomerType());
			map.put("订单编码", customerVO.getOrderNo());
			map.put("订单ID", customerVO.getOrderId());
			map.put("创建订单日期", customerVO.getOrderDate());
			list.add(map);
		}
		HutoolPageUtil.downloadExcel(list, response);
	}


	/**
	 * 获取list
	 *
	 * @return
	 */
	@Override
	public List<OrmCustomerVO> getCustomerList() {
		List<OrmCustomerVO> voList = new ArrayList<>();
		OrmCustomerVO customerVO1 = new OrmCustomerVO();
		customerVO1.setCustomerName("西安客户");
		customerVO1.setCustomerNo("XA01");
		customerVO1.setCustomerId(1001);
		customerVO1.setCustomerAddress("西安市雁塔区天谷八路");
		customerVO1.setCustomerType(1);
		customerVO1.setCustomerFund(BigDecimal.valueOf(12.36));
		customerVO1.setOrderDate(new Date());
		customerVO1.setOrderNo("S0111");
		customerVO1.setOrderId("111");
		customerVO1.setIsValid(1);

		OrmCustomerVO customerVO2 = new OrmCustomerVO();
		customerVO2.setCustomerName("上海客户");
		customerVO2.setCustomerNo("SH01");
		customerVO2.setCustomerId(1002);
		customerVO2.setCustomerAddress("西安市雁塔区天谷八路");
		customerVO2.setCustomerType(2);
		customerVO2.setCustomerFund(BigDecimal.valueOf(22.36));
		customerVO2.setOrderDate(new Date());
		customerVO2.setOrderNo("S0222");
		customerVO2.setOrderId("222");
		customerVO2.setIsValid(1);
		voList.add(customerVO2);
		voList.add(customerVO1);
		return voList;
	}

	/**
	 * 查询单个数据
	 *
	 * @param customerID
	 * @return
	 */
	@Override
	public OrmCustomerVO getCustomerByID(Integer customerID) {
		OrmCustomerVO vo = new OrmCustomerVO();
		OrmCustomerPO customerPOOptional = ormCustomerDao.findOrmCustomerPOById(customerID);

		OrmCustomerVO ormCustomerVO= ormCustomerMapper.queryCustomerById(customerID);
		System.out.println("Mybatis查询的ormCustomerPO 结果"+ JSON.toJSON(ormCustomerVO));

		if (Objects.isNull(customerPOOptional)) {
			return vo;
		}
		BeanUtils.copyProperties(customerPOOptional, vo);
		return vo;
	}

	@Override
	public void jdbcTemplateTest() {
		//1. 查询数据
		String querySql = "SELECT id, customer_name, customer_no, customer_id, customer_address , customer_type, customer_fund, order_date, order_no, order_id FROM orm_customer LIMIT 10;";
		List<Map<String, Object>> mapList = jdbcTemplate.queryForList(querySql);
		System.out.println(mapList);
		//2. 插入数据
		String insertSql = "INSERT INTO `snailthink`.`orm_customer`(`customer_name`, `customer_no`, `customer_id`, `customer_address`, `customer_type`, `customer_fund`, `order_date`, `order_no`, `order_id`, `create_time`, `update_time`, `create_user`, `update_user`, `is_valid`) VALUES ('太仓DC', 'A673', 101, '杭州市滨江区钱江大厦', 1, 0.00000, '2020-11-16 00:00:00', 'SO101', 101, '2021-03-05 18:56:56', '2021-03-05 18:56:56', 0, 0, 1);";
		jdbcTemplate.update(insertSql);
		//3. 更新数据
		String updateSql = "UPDATE orm_customer SET customer_no=?,update_time=? WHERE id=1";
		Object[] objects = new Object[2];
		objects[0] = "test2";
		objects[1] = new Date();
		jdbcTemplate.update(updateSql, objects);
		//4. 删除数据
		Integer deleteId = 100;
		String deleteSql = "delete from `orm_customer` where id=?";
		jdbcTemplate.update(deleteSql, deleteId);
	}

	/**
	 * easy poi 导出excel
	 */
	@Override
	public void exportByEasyPoi(HttpServletResponse response) {
		List<OrmCustomerVO> ormCustomerVOS = getCustomerList();
		List<OrmCustomerExcelVO> excelVOList = BeanUtil.convertList2List(ormCustomerVOS, OrmCustomerExcelVO.class);
		System.out.println("转换数据获取！");
		ExcelUtilsTemp.exportExcelTemp(excelVOList, "标题", "sheetName",
				OrmCustomerExcelVO.class, "客户数据导出.xlsx", true, response);
		System.out.println("执行完成！");
	}

	/**
	 * 下载模板
	 * @param response
	 * @throws IOException
	 */
	@Override
	public void downloadTemplate(HttpServletResponse response) throws IOException {
		//获取static文件夹目录下的文件名称
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/" + "customerExcel.xlsx");
		/**
		 * 下载模板
		 */
		ExcelUtilsTemp.downloadTemplate(response, "customerDemo.xlsx", inputStream);
		System.out.println("下载模板_执行完成！");
	}

	@Override
	public int saveCustomer(OrmCustomerVO customerVO) {
		OrmCustomerPO ormCustomerPO=new OrmCustomerPO();
		BeanUtils.copyProperties(customerVO, ormCustomerPO);
		OrmCustomerPO customerPO= ormCustomerDao.save(ormCustomerPO);
		return customerPO.getId();
	}

	@Override
	public int deleteCustomerId(Long id) {
		ormCustomerDao.deleteById(Integer.valueOf(id.toString()));
		return 1;
	}

	@Override
	public int updateCustomer(Long id, OrmCustomerVO customerVO) {
		OrmCustomerPO ormCustomerPO=new OrmCustomerPO();
		BeanUtils.copyProperties(customerVO, ormCustomerPO);
		OrmCustomerPO customerPO= ormCustomerDao.save(ormCustomerPO);
		return customerPO.getId();
	}

	@Override
	public OrmCustomerVO findCustomerByIds(Long id) {
		OrmCustomerPO ormCustomerPO=ormCustomerDao.findOrmCustomerPOById(Integer.valueOf(id.toString()));

		OrmCustomerVO ormCustomerVO=new OrmCustomerVO();
		BeanUtils.copyProperties(ormCustomerPO, ormCustomerVO);
		return ormCustomerVO;
	}

	@Override
	public List<OrmCustomerVO> listCustomerByPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return null;
	}
}
